package com.ny.controller;

import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import com.ny.service.UserService;
import com.ny.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

//    跳转到申请界面
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ReservationService reservationService;
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession session, Model model) {
        Users user = userService.login(username, password);
        System.out.println(user);
        if (user!= null) {
            session.setAttribute("user", user);
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
            System.out.println(classroomsList);
            model.addAttribute("classroomList", classroomsList);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }
//    多条件查询
    @GetMapping("/searchClassrooms")
    public String searchClassrooms(
            @RequestParam(required = false) String classroomName,
            @RequestParam(required = false) String capacity,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String building,
            @RequestParam(required = false) String description,
            Model model) {
        List<Classrooms> classroomList = classroomService.searchClassrooms(classroomName, capacity, status, building, description);
        System.out.println("获取到的数据"+classroomList);
        model.addAttribute("classroomList", classroomList);
        return "classroom";
    }

//    跳转到申请页面

//    跳转到注册界面
    @RequestMapping("/insertView")
    public String insertView(){

    return "insert";
}

    @RequestMapping("/insert")
    public String insert(Users users){
        int i = userService.allUserName(users);
        if (i>0){
            return "login";
        }
        else {
            return "insert";
        }
    }
//    个人中心页面
    @RequestMapping("/personal")
    public String personal(HttpSession session, Model model) {
        return "personal";
    }
//  我的预约记录
//    @RequestMapping("/reservationRecords")
//    public String reservationRecords(){
//        return "reservationRecords";
//    }

    //    修改手机号码页面
    @RequestMapping("/updatePhone")
    public String updatePhone() {
            return "updatePhone";

    }

//    修改密码页面
    @RequestMapping("/updatePassword")
    public String updatePassword() {
            return "updatePassword";
    }

    // 管理员个人中心页面展示
    @RequestMapping("/center")
    public String adminCenter(HttpSession session, Model model) {
//        Users users = (Users) session.getAttribute("adminUser");
//        if (users == null) {
//            return "redirect:login";
//        }
//        Users currentUser = userService.getUserById(users.getUserId());
//        model.addAttribute("users", currentUser);
        return "center";
    }

    // 管理员修改手机号码
    @RequestMapping("/updateCenPhone")
    public String updatePhone(HttpSession session, @RequestParam("phoneNumber") String phoneNumber, Model model) {
        Users users = (Users) session.getAttribute("adminUser");
        if (users == null) {
            return "redirect:login";
        }
        boolean result = userService.updatePhoneNumber(users.getUserId(), phoneNumber);
        if (result) {
            users.setPhoneNumber(phoneNumber);
            session.setAttribute("adminUser", users);
            model.addAttribute("msg", "手机号码修改成功");
        } else {
            model.addAttribute("msg", "手机号码修改失败");
        }
        return "center";
    }

    // 管理员修改密码
    @RequestMapping("/admin/updateCenPassword")
    public String updatePassword(HttpSession session, @RequestParam("oldPassword") String oldPassword,
                                 @RequestParam("Password") String Password, Model model) {
        Users user = (Users) session.getAttribute("adminUser");
        if (user == null) {
            return "redirect:login";
        }
        // 此处可添加验证旧密码是否正确的逻辑（此处省略简单验证）
        // 根据id查询出的密码跟输入的旧密码是否一样。一样的时候就可以进行修改（执行修改密码的功能，【根据id修改密码】）
        boolean result = userService.updatePassword(user.getUserId(),oldPassword, Password);
        if (result) {
            model.addAttribute("msg", "密码修改成功");
        } else {
            model.addAttribute("msg", "密码修改失败");
        }
        return "center";
    }

    // 查看预约记录页面展示
    @RequestMapping("/reservationRecords")
    public String reservationRecords(Model model) {
        List<Reservations> reservations = reservationService.findAllReservations();
        model.addAttribute("reservations", reservations);
        return "reservationRecords";
    }
}
